package com.heng.locks;

import org.junit.Test;

import java.util.concurrent.TimeUnit;

public class InterruptDemo9 {

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            while(true) {
                if(Thread.currentThread().isInterrupted()){
                    System.out.println("t1线程调用interrupt()后的中断标识：" + Thread.currentThread().isInterrupted() + "程序停止");
                    break;
                }

                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();//
                    //能解决无限循环的问题！
                    e.printStackTrace();
                }
                System.out.println("---hello InterruptDemo");
            }
        }, "t1");
        t1.start();
        try{TimeUnit.MILLISECONDS.sleep(1);}catch (InterruptedException e){e.printStackTrace();}
        new Thread(()->t1.interrupt(),"t2").start();
    }

    @Test
    public void m1() {
        Thread t1 = new Thread(() -> {
            while(true) {
                if(Thread.currentThread().isInterrupted()){
                    System.out.println("t1线程调用interrupt()后的中断标识：" + Thread.currentThread().isInterrupted() + "程序停止");
                    break;
                }
                System.out.println("---hello InterruptDemo");
            }
        }, "t1");
        t1.start();

        try{TimeUnit.MILLISECONDS.sleep(1);}catch (InterruptedException e){e.printStackTrace();}

        new Thread(()->t1.interrupt(),"t2").start();
    }
}
